package com.qf.controller;

import com.qf.entity.Book;
import com.qf.service.BookService;
import com.qf.service.impl.BookServiceImpl;
import oracle.jrockit.jfr.events.RequestableEventEnvironment;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
杨威
2021/9/19
13:38
*/
@WebServlet("/update")
public class UpdateController extends HttpServlet {
    BookService bookService=new BookServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bId = request.getParameter("bId");
        String bTitle = request.getParameter("bTitle");
        String bAuthor = request.getParameter("bAuthor");
        String bPublicDate = request.getParameter("bPublicDate");
        //知识盲区，日期格式必须按照以下格式，月份要大写，否则出来的日期不准！！！
        SimpleDateFormat sif=new SimpleDateFormat("yyyy-MM-dd");
        Date parse=null;
        try {
            parse = sif.parse(bPublicDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String bPublisher = request.getParameter("bPublisher");
        String bIsbn = request.getParameter("bIsbn");
        String bPrice = request.getParameter("bPrice");

        Book book=new Book(Integer.valueOf(bId),bTitle,bAuthor,parse,bPublisher,bIsbn,Double.valueOf(bPrice));

        int i = bookService.updateBook(book);
        if(i>0){
            response.sendRedirect(request.getContextPath()+"/ShowAllBookController");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
